package com.dsdl.chatapp.common.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.dsdl.chatapp.BaseApplication;

/**
 * 临时数据保存工具类
 * Created by zhangkun on 2016/1/19.
 */
public class SPUtil {
    private static SharedPreferences mSharedPreferences;

    private static void init(Context context) {
        if (mSharedPreferences == null) {
            mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        }
    }

    /**
     * 添加Int类型
     *
     * @param key
     * @param value
     */
    public static void setSharedIntData(String key, int value) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        mSharedPreferences.edit().putInt(key, value).commit();
    }

    /**
     * 获取Int类型
     *
     * @param key
     * @return
     */
    public static int getSharedIntData(String key) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return getSharedIntData(key, -1);
    }

    /**
     * 获取Int类型
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getSharedIntData(String key, int defaultValue) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return mSharedPreferences.getInt(key, defaultValue);
    }

    /**
     * 添加Long类型
     *
     * @param key
     * @param value
     */
    public static void setSharedlongData(String key, long value) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        mSharedPreferences.edit().putLong(key, value).commit();
    }

    /**
     * 获取Long类型
     *
     * @param key
     * @return
     */
    public static long getSharedlongData(String key) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return getSharedlongData(key, -1l);
    }

    /**
     * 获取Long类型
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static long getSharedlongData(String key, long defaultValue) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return mSharedPreferences.getLong(key, defaultValue);
    }

    /**
     * 添加Float类型
     *
     * @param key
     * @param value
     */
    public static void setSharedFloatData(String key, float value) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        mSharedPreferences.edit().putFloat(key, value).commit();
    }

    /**
     * 获取Float类型
     *
     * @param key
     * @return
     */
    public static Float getSharedFloatData(String key) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return getSharedFloatData(key, -1f);
    }

    /**
     * 获取Float类型
     *
     * @param key
     * @return
     */
    public static Float getSharedFloatData(String key, float defaultValue) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    /**
     * 添加Boolean类型
     *
     * @param key
     * @param value
     */
    public static void setSharedBooleanData(String key, boolean value) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        mSharedPreferences.edit().putBoolean(key, value).commit();
    }

    /**
     * 获取Boolean类型
     *
     * @param key
     * @return
     */
    public static Boolean getSharedBooleanData(String key) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return getSharedBooleanData(key, false);
    }

    /**
     * 获取Boolean类型
     *
     * @param key
     * @return
     */
    public static Boolean getSharedBooleanData(String key, boolean defaultValue) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    /**
     * 添加String类型
     *
     * @param key
     * @param value
     */
    public static void setSharedStringData(String key, String value) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        mSharedPreferences.edit().putString(key, value).commit();
    }

    /**
     * 获取String类型
     *
     * @param key
     * @return
     */
    public static String getSharedStringData(String key) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return getSharedStringData(key, null);
    }

    /**
     * 获取String类型
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getSharedStringData(String key, String defaultValue) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        return mSharedPreferences.getString(key, defaultValue);
    }

    /**
     * 删除某个键值内容
     *
     * @param key
     */
    public static void remove(Context context, String key) {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        mSharedPreferences.edit().remove(key).commit();
    }

    /**
     * 清空所有
     */
    public static void clearAll() {
        if (mSharedPreferences == null) {
            init(BaseApplication.getInstance());
        }
        mSharedPreferences.edit().clear().commit();
    }
}
